package com.isdust.jy.mbank.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.isdust.jy.mbank.R;
import com.isdust.jy.mbank.activity.HabitatActivity;
import com.isdust.jy.mbank.activity.LoginActivity;
import com.isdust.jy.mbank.adapter.HabitAdapter;
import com.isdust.jy.mbank.base.BaseFragment;
import com.isdust.jy.mbank.view.spinner;
import com.isdust.zor.core.Habitat;
import com.isdust.zor.core.action.ServerAction;
import com.isdust.zor.core.listener.onTaskCallBackListener;
import com.isdust.zor.core.util.PreferenceHelper;
import com.isdust.zor.core.util.SerializableList;

import java.util.ArrayList;
import java.util.List;

public class Main extends BaseFragment {
    private List<Habitat> tmp;
    private final int RESULT_OK = 1;
    private final int RESULT_FAIL = -1;
    private final int REQUEST = 0;
    private HabitAdapter<Habitat> adapter;
    private ListView listView;

    @Override
    protected void onBindViewBefore(View root) {
        super.onBindViewBefore(root);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        super.initData();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sp);
        spinner.drawables.add(bitmap);
        spinner.drawables.add(bitmap);
        spinner.drawables.add(bitmap);
        spinner.drawables.add(bitmap);

        SerializableList<String> temp = new SerializableList<>();
        temp.add("2016-10-3"); //yyyy-M-d
        temp.add("2016-10-1");
        temp.add("2016-10-9");
        temp.add("2016-10-10");
        temp.add("2016-10-11");
        temp.add("2016-10-12");
        String[] temp2 = new String[temp.size()];
        temp.toArray(temp2);

        Habitat habitat = new Habitat("12123123", "习惯测试", "212121", 3, 1, 1, "212121", temp2);

        tmp = app.getHabitats();
        if (tmp == null) {
            tmp = new ArrayList<>();
            tmp.add(habitat);
            app.setHabitats(tmp);
        }
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        listView = (ListView) root.findViewById(R.id.lv_hb_content);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent();
                intent.setClass(mContext, HabitatActivity.class);
                intent.putExtra("location", i);
                startActivityForResult(intent, REQUEST);
            }
        });
       listview();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST:
                switch (resultCode) {
                    case RESULT_OK:
                        Toast.makeText(mContext,"添加成功",Toast.LENGTH_LONG).show();
                        Log.i("habit",app.getHabitats().toString());
                        listview();
                        break;
                    case RESULT_FAIL:
                        break;
                }
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.btn_3:
////               Toast.makeText(getActivity(),AppConfig.SP,Toast.LENGTH_LONG).show();
//                PreferenceHelper tmp=new PreferenceHelper(getActivity());
//                tmp.write("username","admin");
//                tmp.write("password","admin");
//                break;
//            case R.id.btn_4:
//                startActivity(new Intent(getActivity(), LoginActivity.class));
//                break;
//            case R.id.btn_5:
//                Log.i("username",new PreferenceHelper(getActivity()).read("username"));
//            default:
//                break;
        }
    }
    private void listview(){
        adapter = new HabitAdapter<>();
        adapter.setItems(app.getHabitats());
        listView.setAdapter(adapter);

    }
}
